package com.wbao.dianniu.im;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.conversation.IYWConversationService;
import com.alibaba.mobileim.conversation.YWConversation;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.wbao.dianniu.data.NoticeRecData;
import com.wbao.dianniu.listener.INoticeRecListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.NoticeRecListManager;
import com.wbao.dianniu.ui.BaseActivity;
import com.wbao.dianniu.ui.JobDetailsActivity;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.R;

/**
 * 招聘小助手
 * 
 * @author yejianjun
 *
 */
public class RecruitHelperActivity extends BaseActivity implements INoticeRecListListener {
	private PullToRefreshListView pullListView;
	private ListView mListView;
	private NoticeRecListManager manager;
	private MyAdapter adapter;
	private List<NoticeRecData> mList;
	private final int PAGE_COUNTS = 10;
	private int currentPage = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_recruit_helper);
		setTitleName("招聘小助手");
		init();
		requestData(currentPage);
		YWIMKit iMkit = LoginSampleHelper.getInstance().getIMKit();
		IYWConversationService conversationService = iMkit.getConversationService();
		YWConversation conversation = conversationService.getConversationByUserId("115", LoginSampleHelper.APP_KEY);
		if(null != conversation)
			conversationService.markReaded(conversation);
	}
	private void init() {
		pullListView = (PullToRefreshListView) findViewById(R.id.agent_listview);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		mListView = pullListView.getRefreshableView();
		registerForContextMenu(mListView);
		
		adapter = new MyAdapter(this);
		mList = new ArrayList<NoticeRecData>();
		adapter.addData(mList);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(itemClickListener);
	}
	OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.putExtra(GlobalContext.INTENT_JOB_ID, mList.get(position-1).getJobId());
			intent.setClass(RecruitHelperActivity.this, JobDetailsActivity.class);
			startActivity(intent);
		}
	};
	OnRefreshListener onRefreshListener = new OnRefreshListener() {

		@Override
		public void onRefresh() {
			// 上拉刷新 加载请求数据
			pullUpRequest();
		}
	};
	
	// 上拉刷新数据请求
	private void pullUpRequest(){
		//没有搜索结果 或者数据为空 上拉没有效果
		++currentPage ;
		requestData(currentPage);
	}
	
	private void requestData(int page) {
		manager = new NoticeRecListManager(this);
		manager.addNoticeRecListListener(this);
		manager.noticeRecList(GlobalContext.accountId, page * PAGE_COUNTS, PAGE_COUNTS);
	}
	
	class MyAdapter extends BaseAdapter{

		private Context mContext;
		private List<NoticeRecData> list;
		private LayoutInflater mInflater;
		public MyAdapter(Context context) {
			mContext = context;
			mInflater = LayoutInflater.from(mContext);
			if(null == list)
				list = new ArrayList<NoticeRecData>();
		}
		
		public void addData(List<NoticeRecData> addList){
			for (int i = 0; i < addList.size(); i++) {
				this.list.add(addList.get(i));
			}
		}
		public List<NoticeRecData> getList() {
			return list;
		}
		@Override
		public int getCount() {
			if (null == list)
				return 0;
			else
				return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.recruit_msg_item, null);   
				holder = new Holder();
				holder.time = (TextView) convertView.findViewById(R.id.recruit_msg_time);
				holder.head = (ImageView)convertView.findViewById(R.id.user_head);
				holder.name =  (TextView)convertView.findViewById(R.id.user_name);
				holder.label = (TextView) convertView.findViewById(R.id.user_label);
				holder.post =  (TextView)convertView.findViewById(R.id.recruit_msg_post);
				convertView.setTag(holder); 
			}else{
				 holder = (Holder) convertView.getTag();   
			}
			final NoticeRecData data = list.get(position);
			PictureDownloadUtils.displayHeadImage(mContext,holder.head, data.getHeadPic());
			holder.time.setText(data.getCreateDate() == null ? "" : data.getCreateDate());
			holder.name.setText( data.getRealName() == null ? "" : data.getRealName());
			holder.label.setText(data.getLabel() == null ? "" : data.getLabel());
			holder.post.setText(data.getPost() == null ? "" : data.getPost());
			return convertView;
		}
		
		class Holder{
			public TextView time;
			public ImageView head;
			public TextView name;
			public TextView label;
			public ImageView sex;
			public TextView post;
		}
		
	}

	@Override
	public void onNoticeRecListFailure(int errorCode, String errorMsg) {
		// TODO Auto-generated method stub
		pullListView.onRefreshComplete(false, false);
	}
	@Override
	public void onNoticeRecListSuccess(List<NoticeRecData> list) {
		pullListView.onRefreshComplete(false, false);
		if(null == list)
			return;
		adapter.addData(list);
		mList = adapter.getList();
		adapter.notifyDataSetChanged();
	}
	
}
